package com.tongtong.stsuserapi.controller;

import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.proxy.annotation.AutoGetId;
import com.tongtong.stsuserapi.pojo.User;
import com.tongtong.stsuserapi.service.base.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户控制器
 */
@Api(tags="用户接口")
@RestController
@RequestMapping("/sts/user")
public class UserController {

	@Autowired
	UserService userService;

	@ApiOperation("获取用户信息")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@GetMapping("/info")
	@AutoGetId
	public STSMessage<User> getInfoById(Long id) {
		return userService.getInfoById(id);
	}

	@ApiOperation(value = "更新用户信息", notes = "手机号和密码字段会被自动屏蔽")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PatchMapping("/info")
	@AutoGetId
	public STSMessage UpdateInfoById(Long id, @RequestBody User user) {
		return userService.updateUserById(id,user);
	}

	@ApiOperation("更新用户头像")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PatchMapping("/head")
	@AutoGetId
	public STSMessage UpdateHeadById(Long id, @RequestPart("file") MultipartFile file) throws Exception {
		return userService.updateHeadById(id,file);
	}

	@ApiOperation("上传付款码")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PatchMapping("/code")
	@AutoGetId
	public STSMessage UploadCodeById(Long id, @RequestPart("file") MultipartFile file) throws Exception {
		return userService.uploadCollectionCode(id,file);
	}

	@ApiOperation(value="更新用户密码",notes="验证旧密码通过后，修改为新密码")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PatchMapping("/password")
	@AutoGetId
	public STSMessage UpdatePasswordById(Long id, String oldPassword, String newPassword) {
		return userService.updatePasswordById(id,oldPassword,newPassword);
	}

	@ApiOperation(value="更新用户手机号",notes="需要先申请短信验证码，验证通过后修改为新手机号")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PatchMapping("/phone")
	@AutoGetId
	public STSMessage updatePhoneById(Long id, String oldPhone, String newPhone, String captcha) {
		return userService.updatePhoneById(id, oldPhone, newPhone, captcha);
	}

	@ApiOperation(value="注销账户",notes="该接口无需传入id参数，用户id直接从token中获取")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@DeleteMapping("/delete")
	@AutoGetId
	public STSMessage deleteUser(Long id) {
		return userService.deleteUserById(id);
	}
}
